package yf.mall.controller.api;

import com.yf.annotation.LoginUserId;
import yf.mall.entity.CartEntity;
import yf.mall.entity.MallGoodsEntity;
import yf.mall.entity.PriceEntity;
import yf.mall.enums.MallGoodsSaleTypeEnum;
import com.yf.response.R;
import yf.mall.service.*;
import yf.mall.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.vo.ItemVo;
import yf.mall.vo.TicketVo;

import java.math.BigDecimal;
import java.util.List;

@RequestMapping("/api/cart")
@RestController
public class ApiCartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private PriceService priceService;

    @Autowired
    private MallOrderService mallOrderService;

    @Autowired
    private MallGoodsService goodsService;

    @Autowired
    private MallActService actService;


    //TODO 暂时不分页
    @GetMapping("/list")
    public R info(@LoginUserId Long userId) {
        List<CartVo> cartVos = cartService.findByUserId(userId);
        //计算级别价格
        cartVos.forEach(cartVo -> {
            MallGoodsEntity goodsEntity = goodsService.get(cartVo.getGoodsId());
            TicketVo ticketVo = actService.getTicketVo(goodsEntity.getId());
            //设置积分
            cartVo.setPoint(ticketVo.getPoint());
            //设置优惠券
            cartVo.setCoupon(ticketVo.getCoupon());
            //是代理产品 获取代理价格
            if (cartVo.getGoodsSaleType().equals(MallGoodsSaleTypeEnum.AGENT.getIndex())) {
                PriceEntity priceEntity = priceService.findByUserIdAndGoodsId(userId, cartVo.getGoodsId());
                if (priceEntity != null) {//设置代理价格
                    cartVo.setGoodsNowPrice(priceEntity.getPrice());
                }
            }
        });
        return R.ok(cartVos);
    }




    //计算物流费
    @RequestMapping("/logFee")
    public R logFee(@LoginUserId Long userId, String province) {
        List<CartEntity> cartEntities = cartService.findByUserIdWithSelected(userId);
        BigDecimal bigDecimal = mallOrderService.computeLogFee(cartEntities, province);
        return R.ok(bigDecimal);
    }


    /**
     * 修改购物车数量
     *
     * @param cartId
     * @param amount
     * @return
     */
    @PostMapping("/change")
    public R change(@LoginUserId Long userId, Long cartId, Integer amount, Boolean selected) {
        cartService.change(userId, cartId, amount, selected);
        return R.ok();
    }

    /**
     * 新增购物车
     *
     * @param userId
     * @param goodsId
     * @param skuName
     * @return
     */
    @PostMapping("/add")
    public R addCart(@LoginUserId Long userId, Long goodsId, String skuName, Integer quantity) {
        cartService.addCart(userId, goodsId, skuName, quantity);
        return R.ok();
    }

    /**
     * 删除购物车
     *
     * @param userId
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public R deleteCart(@LoginUserId Long userId, Long id) {
        CartEntity cartEntity = cartService.get(id);
        //只能删除自己的
        if (cartEntity.getUserId().equals(userId)) {
            cartService.delete(id);
        } else {
            return R.error("非法操作!");
        }
        return R.ok();
    }

    /**
     * 清空购物车
     *
     * @param userId
     * @param
     * @return
     */
    @PostMapping("/all/delete")
    public R deleteAll(@LoginUserId Long userId) {
        cartService.deleteAll(userId);
        return R.ok();
    }


    /**
     * 全选购物车
     *
     * @param userId
     * @param id
     * @return
     */
    @PostMapping("/all/selected")
    public R chaneAllSelected(@LoginUserId Long userId, boolean selected) {
        cartService.changeAllSelected(userId, selected);
        return R.ok();
    }

}
